/*
 * SequenceEditorPanel.java
 *
 * Created on 1 de septiembre de 2008, 22:07
 */

package r4p_gui;

import java.util.ArrayList;
import r4p.modules.servomotors.Instant;
import r4p.modules.servomotors.Position;
import r4p.modules.servomotors.Sequence;
/**
 *
 * @author  nemesis
 */
public class SequenceEditorPanel extends javax.swing.JPanel {

    Sequence sequence;
    
    /** Creates new form SequenceEditorPanel */
    public SequenceEditorPanel() {
        initComponents();
	this.sequence = new Sequence();
    }

    public void setTime(int time) {
	this.sliderTimeLine.setValue(time);
    }
    
    public void setValues(int[] values) {
	this.positionEditor1.setValue(values[0]);
	this.positionEditor2.setValue(values[1]);
	this.positionEditor3.setValue(values[2]);
	this.positionEditor4.setValue(values[3]);
	this.positionEditor5.setValue(values[4]);
	this.positionEditor6.setValue(values[5]);
	this.positionEditor7.setValue(values[6]);
	this.positionEditor8.setValue(values[7]);
	this.positionEditor9.setValue(values[8]);
	this.positionEditor10.setValue(values[9]);
	this.positionEditor11.setValue(values[10]);
	this.positionEditor12.setValue(values[11]);
    }
    
    public int[] getValues() {
	int[] values = new int[12];
	
	values[0] = this.positionEditor1.getValue();
	values[1] = this.positionEditor2.getValue();
	values[2] = this.positionEditor3.getValue();
	values[3] = this.positionEditor4.getValue();
	values[4] = this.positionEditor5.getValue();
	values[5] = this.positionEditor6.getValue();
	values[6] = this.positionEditor7.getValue();
	values[7] = this.positionEditor8.getValue();
	values[8] = this.positionEditor9.getValue();
	values[9] = this.positionEditor10.getValue();
	values[10] = this.positionEditor11.getValue();
	values[11] = this.positionEditor12.getValue();
	
	return values;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        positionEditor1 = new r4p_gui.PositionEditor();
        positionEditor2 = new r4p_gui.PositionEditor();
        positionEditor3 = new r4p_gui.PositionEditor();
        positionEditor4 = new r4p_gui.PositionEditor();
        positionEditor5 = new r4p_gui.PositionEditor();
        positionEditor6 = new r4p_gui.PositionEditor();
        positionEditor7 = new r4p_gui.PositionEditor();
        positionEditor8 = new r4p_gui.PositionEditor();
        positionEditor9 = new r4p_gui.PositionEditor();
        positionEditor10 = new r4p_gui.PositionEditor();
        positionEditor11 = new r4p_gui.PositionEditor();
        positionEditor12 = new r4p_gui.PositionEditor();
        button_goToInstant = new javax.swing.JButton();
        sliderTimeLine = new javax.swing.JSlider();
        spinnerTimeLine = new javax.swing.JSpinner();

        positionEditor1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Servo 1", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Sans", 0, 10))); // NOI18N

        positionEditor2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Servo 2", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Sans", 0, 10))); // NOI18N

        positionEditor3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Servo 3", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Sans", 0, 10))); // NOI18N

        positionEditor4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Servo 4", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Sans", 0, 10))); // NOI18N

        positionEditor5.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Servo 5", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Sans", 0, 10))); // NOI18N

        positionEditor6.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Servo 6", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Sans", 0, 10))); // NOI18N

        positionEditor7.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Servo 7", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Sans", 0, 10))); // NOI18N

        positionEditor8.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Servo 8", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Sans", 0, 10))); // NOI18N

        positionEditor9.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Servo 9", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Sans", 0, 10))); // NOI18N

        positionEditor10.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Servo 10", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Sans", 0, 10))); // NOI18N

        positionEditor11.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Servo 11", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Sans", 0, 10))); // NOI18N

        positionEditor12.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Servo 12", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Sans", 0, 10))); // NOI18N

        button_goToInstant.setIcon(new javax.swing.ImageIcon("/home/nemesis/NetBeansProjects/R4P_gui/icons/goto.png")); // NOI18N
        button_goToInstant.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                button_goToInstantMouseClicked(evt);
            }
        });

        sliderTimeLine.setMaximum(10000);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, spinnerTimeLine, org.jdesktop.beansbinding.ELProperty.create("${value}"), sliderTimeLine, org.jdesktop.beansbinding.BeanProperty.create("value"));
        bindingGroup.addBinding(binding);

        sliderTimeLine.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                sliderTimeLineMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                sliderTimeLineMouseReleased(evt);
            }
        });
        sliderTimeLine.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sliderTimeLineStateChanged(evt);
            }
        });

        spinnerTimeLine.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                spinnerTimeLineInputMethodTextChanged(evt);
            }
        });
        spinnerTimeLine.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                spinnerTimeLineKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(sliderTimeLine, javax.swing.GroupLayout.PREFERRED_SIZE, 692, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(spinnerTimeLine, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(button_goToInstant))
            .addGroup(layout.createSequentialGroup()
                .addComponent(positionEditor1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(positionEditor2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(positionEditor3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(positionEditor4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(positionEditor5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(positionEditor6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(positionEditor7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(positionEditor8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(positionEditor9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(positionEditor10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(positionEditor11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(positionEditor12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(button_goToInstant, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(spinnerTimeLine, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(sliderTimeLine, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(positionEditor1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(positionEditor2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(positionEditor3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(positionEditor4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(positionEditor5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(positionEditor6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(positionEditor7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(positionEditor8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(positionEditor9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(positionEditor10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(positionEditor11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(positionEditor12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

private void button_goToInstantMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button_goToInstantMouseClicked
    // TODO add your handling code here:
    Instant instant = new Instant(this.sliderTimeLine.getValue());
    instant.addPosition(0, this.positionEditor1.getValue());
    instant.addPosition(1, this.positionEditor2.getValue());
    instant.addPosition(2, this.positionEditor3.getValue());
    instant.addPosition(3, this.positionEditor4.getValue());
    instant.addPosition(4, this.positionEditor5.getValue());
    instant.addPosition(5, this.positionEditor6.getValue());
    instant.addPosition(6, this.positionEditor7.getValue());
    instant.addPosition(7, this.positionEditor8.getValue());
    instant.addPosition(8, this.positionEditor9.getValue());
    instant.addPosition(9, this.positionEditor10.getValue());
    instant.addPosition(10, this.positionEditor11.getValue());
    instant.addPosition(11, this.positionEditor12.getValue());
    
    this.sequence.addInstant(instant);
    System.out.println("Instant added: " + instant);
//    Instant instant = this.sequence.getInstantByTimeMark(this.timeLineEditor1.getValue());
//    ArrayList<Position> auxPos = instant.getPositions();
//    int[] pos = new int[12];
//    
//    for (int i = 0; i < auxPos.size(); i++) {
//	pos[i] = auxPos.get(i).getValue();
//    }
//    
//    this.setValues(pos);
    

}//GEN-LAST:event_button_goToInstantMouseClicked

private void sliderTimeLineStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderTimeLineStateChanged
    // TODO add your handling code here:
}//GEN-LAST:event_sliderTimeLineStateChanged

private void sliderTimeLineMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sliderTimeLineMouseClicked
    // TODO add your handling code here:
}//GEN-LAST:event_sliderTimeLineMouseClicked

private void sliderTimeLineMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sliderTimeLineMouseReleased
    // TODO add your handling code here:
    this.timeChanged();
}//GEN-LAST:event_sliderTimeLineMouseReleased

public void timeChanged() {
    System.out.println("RELEASED!!!! " + this.sliderTimeLine.getValue());
    int[] pos = new int[12];
    for (int i = 0; i < pos.length; i++) {
	pos[i] = 0;
    }
    
    Instant instant = this.sequence.getInstantByTimeMark(this.sliderTimeLine.getValue());
    if (instant != null) {
	ArrayList<Position> auxPos = instant.getPositions();

	if (!auxPos.isEmpty()) {
	    for (int i = 0; i < auxPos.size(); i++) {
		pos[i] = auxPos.get(i).getValue();
	    }
	}
    }
    
    this.setValues(pos);
}

private void spinnerTimeLineKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_spinnerTimeLineKeyTyped
    // TODO add your handling code here:
}//GEN-LAST:event_spinnerTimeLineKeyTyped

private void spinnerTimeLineInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_spinnerTimeLineInputMethodTextChanged
    // TODO add your handling code here:
}//GEN-LAST:event_spinnerTimeLineInputMethodTextChanged


public void aux() {
    
    
    //////
    
    
}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton button_goToInstant;
    private r4p_gui.PositionEditor positionEditor1;
    private r4p_gui.PositionEditor positionEditor10;
    private r4p_gui.PositionEditor positionEditor11;
    private r4p_gui.PositionEditor positionEditor12;
    private r4p_gui.PositionEditor positionEditor2;
    private r4p_gui.PositionEditor positionEditor3;
    private r4p_gui.PositionEditor positionEditor4;
    private r4p_gui.PositionEditor positionEditor5;
    private r4p_gui.PositionEditor positionEditor6;
    private r4p_gui.PositionEditor positionEditor7;
    private r4p_gui.PositionEditor positionEditor8;
    private r4p_gui.PositionEditor positionEditor9;
    private javax.swing.JSlider sliderTimeLine;
    private javax.swing.JSpinner spinnerTimeLine;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
